import 'package:demo202112/utils/common_appbar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_elves/flutter_blue_elves.dart';
/// @Author wywinstonwy
/// @Date 2022/7/23 17:15
/// @Description: 

class BluePage extends StatefulWidget {
  const BluePage({Key? key}) : super(key: key);

  @override
  State<BluePage> createState() => _BluePageState();
}

class _BluePageState extends State<BluePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar("蓝牙相关"),
      body: Column(
        children: [
          ElevatedButton(child: Text("检查蓝牙"),onPressed: (){
            checcheBlue();
          },),
          ElevatedButton(child: Text("使用蓝牙"),onPressed: (){
            runBlue();
          },),
        ],
      )
    );
  }
  void checcheBlue(){
        ///Android:
        FlutterBlueElves.instance.androidCheckBlueLackWhat().then((values) {
          print(values);

          if(values.contains(AndroidBluetoothLack.bluetoothPermission)){
            ///no bluetooth permission,if your target is android 12
          }
          if(values.contains(AndroidBluetoothLack.locationPermission)){
            ///no location permission
          }
          if(values.contains(AndroidBluetoothLack.locationFunction)){
            ///location powerOff
          }
          if(values.contains(AndroidBluetoothLack.bluetoothFunction)){
            ///bluetooth powerOff
          }
        });

        ///Ios:
        FlutterBlueElves.instance.iosCheckBluetoothState().then((value) {
          print(value);
          if(value==IosBluetoothState.unKnown){
            ///Bluetooth is not initialized
          }else if(value==IosBluetoothState.resetting){
            ///Bluetooth is resetting
          }else if(value==IosBluetoothState.unSupport){
            ///Bluetooth not support
          }else if(value==IosBluetoothState.unAuthorized){
            ///No give bluetooth permission
          }else if(value==IosBluetoothState.poweredOff){
            ///bluetooth powerOff
          }else{
            ///bluetooth is ok
          }
        });
    }

  void runBlue(){
    FlutterBlueElves.instance.androidCheckBlueLackWhat().then((values) {
      if (values.toString() == '[]') {
        // concact();   // 这里判断是否连接上蓝牙
      } else {
        if (values.contains(AndroidBluetoothLack.bluetoothPermission)) {
          print("没有授予蓝牙权限");
          FlutterBlueElves.instance.androidApplyBluetoothPermission((isOk) {
            print(isOk ? "用户同意开启蓝牙权限" : "用户不同意开启蓝牙权限");
          });
        }
        if (values.contains(AndroidBluetoothLack.locationPermission)) {
          print("没有授予定位权限");
          FlutterBlueElves.instance.androidApplyLocationPermission((isOk) {
            print(isOk ? "用户同意授予位置权限" : "用户不同意授予位置权限");
          });
        }
        if (values.contains(AndroidBluetoothLack.locationFunction)) {
          print("没有开启定位功能");
          FlutterBlueElves.instance.androidOpenLocationService((isOk) {
            print(isOk ? "用户同意开启定位功能" : "用户不同意开启定位功能");
          });
        }
        if (values.contains(AndroidBluetoothLack.bluetoothFunction)) {
          print("没有开启蓝牙功能");
          FlutterBlueElves.instance.androidOpenBluetoothService((isOk) {
            if (isOk) {
              print('开启蓝牙了');
              // concact();
            }
          });
        }
      }
    });


  }

}

